/**
 * 
 */
package com.shy.cms.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shy.base.controller.AbsController;
import com.shy.base.util.BeanUtil;
import com.shy.base.vo.AppConstants;
import com.shy.base.vo.EasyuiPagerVo;
import com.shy.base.vo.PagerFilter;
import com.shy.cms.entity.Site;
import com.shy.cms.service.SiteService;

/**
 * <p>Title:SiteController </p>
 * <p>Description: 站点信息 controller层</p>
 * <p>Company:shy </p> 
 * @author H2013788
 * @date 2019年08月13日
 */
@Controller
@RequestMapping("site")
public class SiteController extends AbsController {
	
	/**
     * log
     */
	private Logger logger = LogManager.getLogger(this.getClass());

	/**
	 * service层
	 */
    @Resource
    private SiteService siteService;

	/**
	 * 加载页面
	 * @return
	 * @author H2013788
	 * @date 2019年08月13日
	 */
	@RequestMapping("page")
	public String loadPage() {
		return "cms/site_list";
	}
	
	/**
	 * 加载数据
	 * 
	 * @author H2013788
	 * @date 2019年08月13日
	 */
	@RequestMapping("data")
	public void loadData(Site site, Integer page, Integer rows, HttpServletResponse response) {
		PagerFilter pagerFilter = getPageable(rows == null ? 15L : rows);
        pagerFilter.setPagePer(null == page ? 1 : page);
        
        //String[] likes = { "filed1", "filed2" }; 
        //Map<String, String[]> filterMap = new HashMap<>();
        //filterMap.put(AppConstants.LIKE_TYPES, likes);
        
        pagerFilter = siteService.listByPager(site, null, pagerFilter);
        //输出json
        writeResponse(new StringBuffer(gsonDateTime.toJson(new EasyuiPagerVo(pagerFilter))), response);
	}
	
	/**
	 * 编辑
	 * 
	 * @author H2013788
	 * @date 2019年08月13日
	 */
	@RequestMapping("edit")
	public void eidt(Integer id, HttpServletResponse response) {
		if (null != id) {
			Site site = siteService.getById(id);
            //输出json
            writeResponse(new StringBuffer(gsonDateTime.toJson(site)), response);
        } else {
            respEasyuiErrorResult(AppConstants.configMap.get(AppConstants.GET_OBJECT_NULL), response);
        }
	} 
	
	
	/**
	 * 保存
	 * 
	 * @author H2013788
	 * @date 2019年08月13日
	 */
	@RequestMapping("save")
	public void save(Site site, HttpServletResponse response) {
		if (null == site) {
            respEasyuiErrorResult(AppConstants.configMap.get(AppConstants.REQUEST_PARAM_NULL), response);
        } else {
            try {
                if (null != site.getId()) {
                	//编辑
                	site.setUpdateTime(new Date());
                	site.setUpdateUser(getShiroUser().getLoginName());
                	Site old = siteService.getById(site.getId());
                    BeanUtil.merge(old, site);
                    siteService.update(old);
                    respEasyuiSuccessResult(AppConstants.configMap.get(AppConstants.SAVE_SUCCESS), response);
                } else {
					site.setCreateUser(getShiroUser().getLoginName());
					site.setCreateTime(new Date());
					siteService.save(site);
					
					respEasyuiSuccessResult(AppConstants.configMap.get(AppConstants.SAVE_SUCCESS), response);
                }
            } catch (Exception e) {
                e.printStackTrace();
                respEasyuiErrorResult(AppConstants.configMap.get(AppConstants.SAVE_ERROR), response);
                logger.error(AppConstants.configMap.get(AppConstants.SAVE_ERROR) + e);
            }
        }
	}
	
	/**
	 * 刪除数据
	 * 
	 * @author H2013788
	 * @date 2019年08月13日
	 */
	@RequestMapping("delete")
	public void delete(Integer id, HttpServletResponse response) {
		try {
            if (null != id) {
            	siteService.deleteById(id);
                respEasyuiSuccessResult(AppConstants.configMap.get(AppConstants.OPERATE_SUCCESS), response);
            } else {
                respEasyuiErrorResult(AppConstants.configMap.get(AppConstants.REQUEST_PARAM_NULL), response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            respEasyuiErrorResult(AppConstants.configMap.get(AppConstants.OPERATE_ERROR), response);
            logger.error(AppConstants.configMap.get(AppConstants.DELETE_ERROR) + e);
        }
	}
}
